<?php

function In_Fichier($contenu){
	$fichier = fopen("blog.xml","w+");
	fwrite($fichier,$contenu);
	fclose($fichier);
}
function debug($contenu){
	echo "<pre>".$contenu."<pre>";
}
$db_address = "localhost";
$db_bdd = "blog";
$db_login = "root";
$db_pass = "metalgun";
$connexion = "mysql:dbname=" . $db_bdd . ";host=" . $db_address . ";";
try{
	$c = new PDO($connexion,$db_login,$db_pass);
}
catch(Exception $e){
	echo "Message D'erreur : ".$e->getMessage();
	die();
}
$total  ="<?xml version='1.0' encoding='UTF-8'?>\n";
$total .="<?xml-stylesheet href='view.xsl' type='text/xsl'?>\n";
$total .="<ROOT>\n";


$total .="\t<users>\n";

$response = $c->query("SELECT * FROM users");

while($data = $response->fetch(PDO::FETCH_ASSOC)){
	$total .="\t\t<user id='".$data["id"]."'>\n";
	$total .="\t\t\t<username>".$data['username']."</username>\n";
	$total .="\t\t\t<password>".$data['password']."</password>\n";
	$total .="\t\t\t<age>".$data['age']."</age>\n";
	$total .="\t\t</user>\n";
	
}
$total .="\t</users>\n";

$total .="\t<news>\n";
$response = $c->query("SELECT * FROM news");

while($data = $response->fetch(PDO::FETCH_ASSOC)){
	$total .="\t\t<new id='".$data["id"]."'>\n";
	$total .="\t\t\t<title>".$data['title']."</title>\n";
	$total .="\t\t\t<body>".$data['body']."</body>\n";
	$total .="\t\t\t<date>".$data['date_time']."</date>\n";
	$total .="\t\t\t<id_user>".$data['id_user']."</id_user>\n";
	$total .="\t\t</new>\n";
	
}
$total .="\t</news>\n";

$total .="\t<comments>\n";
$response = $c->query("SELECT * FROM comments");

while($data = $response->fetch(PDO::FETCH_ASSOC)){
	$total .="\t\t<comment id='".$data["id"]."'>\n";
	$total .="\t\t\t<body>".$data['body']."</body>\n";
	$total .="\t\t\t<date>".$data['date_time']."</date>\n";
	$total .="\t\t\t<id_user>".$data['id_user']."</id_user>\n";
	$total .="\t\t\t<id_news>".$data['id_news']."</id_news>\n";
	$total .="\t\t</comment>\n";
	
}
$total .="\t</comments>\n";


$total .="</ROOT>\n";
In_Fichier($total);
?>